/**
  @file: LogFormatter.java

  PURPOSE: 
  Defines a custom line formatter for the Log class.

  @author Bill Kraemer

  REVISION HISTORY ABSTRACT

  PROGRAMMER:  Bill Kraemer         ORG: MSE

  Copyright 2011 Mission Solutions LLC. All rights reserved.

  END ABSTRACT
 */
package com.mse.utilities;

import java.text.MessageFormat;
import java.util.logging.LogRecord;
import java.util.Date;

/**
  DESCRIPTION:
  This class provides a customized line formatter for the Log class.
 */
public class LogFormatter extends java.util.logging.Formatter {
   
   ///< log line format definition
   private static final MessageFormat messageFormat = 
      new MessageFormat("{0,date,yyyy-MM-dd HH:mm:ss:SSS} {1} : {2} {3} {4}\n");
   
   /**
     DESCRIPTION:
     Constructs an instance of LogFormatter.
    */
   public LogFormatter () {
      super();
   }
   
   /**
     DESCRIPTION:
     This method defines the arguments to be displayed in each log entry.
     <ul>
     <li>argument 0 = time stamp</li>
     <li>argument 1 = thread name string</li>
     <li>argument 2 = entry type string ("DEBUG","ERROR", etc)</li>
     <li>argument 3 = message string</li>
     <li>argument 4 = stack trace string</li>
     </ul>
   
     @param record      the log record generated by the Logger method
     @return            formatted log string 
     @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
    */
   @Override
   public String format(LogRecord record) {
      Object[] arguments = new Object[5];
      Object[] parameters = record.getParameters();
      arguments[0] = new Date(record.getMillis());
      arguments[1] = Thread.currentThread().getName();
      arguments[2] = record.getMessage();
      arguments[3] = "";
      arguments[4] = "";
      if ((parameters != null) && (parameters.length == 2)) {
         arguments[2] = parameters[0];
         arguments[3] = record.getMessage();
         arguments[4] = parameters[1];
      }
      return messageFormat.format(arguments);
   }

}
